import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { SystemInfoService } from './system-info.service';
import { CreateTaskDto } from './dto/create-task.dto';
import { UpdateTaskDto } from './dto/update-task.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { PermissionsGuard } from '../common/guards/permissions.guard';
import { Permissions } from '../common/decorators/permissions.decorator';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';

@ApiTags('系统管理')
@ApiBearerAuth()
@Controller('system-info')
@UseGuards(JwtAuthGuard, PermissionsGuard)
export class SystemInfoController {
  constructor(private readonly systemInfoService: SystemInfoService) {}

  // 系统信息
  @Get('system')
  @Permissions('system:read')
  @ApiOperation({ summary: '获取系统信息', description: '获取系统基本信息如CPU、内存等' })
  @ApiResponse({ status: 200, description: '返回系统基本信息' })
  getSystemInfo() {
    return this.systemInfoService.getSystemInfo();
  }

  // 数据库信息
  @Get('database')
  @Permissions('system:read')
  @ApiOperation({ summary: '获取数据库信息', description: '获取MongoDB数据库信息和统计数据' })
  @ApiResponse({ status: 200, description: '返回数据库信息' })
  getDatabaseInfo() {
    return this.systemInfoService.getDatabaseInfo();
  }

  // Redis 管理
  @Get('redis/keys')
  @Permissions('system:read')
  @ApiOperation({ summary: '获取Redis键列表', description: '根据模式获取Redis中的键' })
  @ApiResponse({ status: 200, description: '返回Redis键列表' })
  getRedisKeys(@Query('pattern') pattern: string) {
    return this.systemInfoService.getRedisKeys(pattern);
  }

  @Get('redis/values/:key')
  @Permissions('system:read')
  @ApiOperation({ summary: '获取Redis键值', description: '获取指定键的值' })
  @ApiResponse({ status: 200, description: '返回Redis键值' })
  getRedisValue(@Param('key') key: string) {
    return this.systemInfoService.getRedisValue(key);
  }

  @Post('redis/values')
  @Permissions('system:write')
  @ApiOperation({ summary: '设置Redis键值', description: '设置Redis键值对' })
  @ApiResponse({ status: 201, description: '键值设置成功' })
  setRedisValue(
    @Body('key') key: string,
    @Body('value') value: any,
    @Body('ttl') ttl?: number
  ) {
    return this.systemInfoService.setRedisValue(key, value, ttl);
  }

  @Delete('redis/keys/:key')
  @Permissions('system:write')
  @ApiOperation({ summary: '删除Redis键', description: '删除指定的Redis键' })
  @ApiResponse({ status: 200, description: '键删除成功' })
  deleteRedisKey(@Param('key') key: string) {
    return this.systemInfoService.deleteRedisKey(key);
  }

  // 定时任务管理
  @Get('tasks')
  @Permissions('system:read')
  @ApiOperation({ summary: '获取所有任务', description: '获取所有定时任务列表' })
  @ApiResponse({ status: 200, description: '返回任务列表' })
  getAllTasks() {
    return this.systemInfoService.getAllTasks();
  }

  @Get('tasks/:id')
  @Permissions('system:read')
  @ApiOperation({ summary: '获取单个任务', description: '根据ID获取定时任务详情' })
  @ApiResponse({ status: 200, description: '返回任务信息' })
  getTask(@Param('id') id: string) {
    return this.systemInfoService.getTask(id);
  }

  @Post('tasks')
  @Permissions('system:write')
  @ApiOperation({ summary: '创建任务', description: '创建新的定时任务' })
  @ApiResponse({ status: 201, description: '任务创建成功' })
  createTask(@Body() createTaskDto: CreateTaskDto) {
    return this.systemInfoService.createTask(createTaskDto);
  }

  @Patch('tasks/:id')
  @Permissions('system:write')
  @ApiOperation({ summary: '更新任务', description: '更新定时任务信息' })
  @ApiResponse({ status: 200, description: '任务更新成功' })
  updateTask(@Param('id') id: string, @Body() updateTaskDto: UpdateTaskDto) {
    return this.systemInfoService.updateTask(id, updateTaskDto);
  }

  @Delete('tasks/:id')
  @Permissions('system:write')
  @ApiOperation({ summary: '删除任务', description: '删除定时任务' })
  @ApiResponse({ status: 200, description: '任务删除成功' })
  deleteTask(@Param('id') id: string) {
    return this.systemInfoService.deleteTask(id);
  }
} 